<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button type="primary" @click="add">新增角色</el-button>
          <el-table :data="list">
            <el-table-column width="120px">
              <template v-slot="{ $index }">
                <div>
                  <!-- {{ (page.page - 1) * page.pageSize + $index + 1 }} -->
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="角色名" />
            <el-table-column prop="description" label="描述" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button type="text" @click="setPower(row)">
                    分配权限
                  </el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="myPage">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 2, 10, 100]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="sizeChange"
              @current-change="currChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <component :is="'Info'" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <Dialog
      ref="dialogItem"
      v-model="show"
      :mode="mode"
      :list="list"
      @currChange="currChange"
      @getData="getData"
    />
    <DialogPower ref="power" :is-show.sync="isShow" @getData="getData" />
  </div>
</template>
<script>
import Dialog from './components/dialog.vue'
import DialogPower from './components/dialogPower.vue'
import { sysRole, sysRoleDelete } from '@/api/setting.js'
export default {
  name: '',
  components: {
    Dialog,
    Info: () => import('./components/info.vue'),
    DialogPower
  },
  data() {
    return {
      mode: 'add',
      show: false,
      isShow: false,
      page: {
        pagesize: 10,
        total: 100,
        page: 1
      },
      list: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取所有角色
    async getData() {
      const res = await sysRole(this.page)
      console.log('角色', res)
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    // 页容量改变时
    sizeChange(size) {
      this.page.pagesize = size
      // 回到第一页
      this.page.page = 1
      this.getData()
    },
    // 点击页码时切换到目标页
    currChange(page) {
      this.page.page = page
      this.getData()
    },
    // 新增角色
    add() {
      this.mode = 'add'
      this.show = true
    },
    // 编辑角色
    edit(row) {
      this.mode = 'edit'
      this.show = true
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
    },
    // 删除角色
    del(row) {
      this.$confirm('确定删除这个角色吗', '提示')
        .then(async() => {
          await sysRoleDelete(row.id)
          this.$message.success('删除成功')
          this.currChange(1)
        })
        .catch(() => {})
    },
    // 分配角色
    setPower(row) {
      this.isShow = true
      this.$refs.power.getRoleInfo(row.id)
    }
  }
}
</script>
<style scoped lang="scss">
.setting {
  padding: 15px;
  .myPage {
    text-align: center;
    padding-top: 10px;
  }
}
</style>
